/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



Ext.define('Intranet.Desktop.view.Login', {
    extend: 'Ext.form.Panel',
    requires: [
        'Intranet.Desktop.view.MainScreen'
    ],
    bodyPadding: 10,
    border: false,
    defaultType: 'textfield',
    items: [{
            fieldLabel: 'Username',
            name: 'username',
            allowBlank: false,
//                    listeners: {
//                        specialkey: function(field, event) {
//                            if (event.getKey() === event.ENTER) {
//                                var form = field.up('form').getForm();
//                                form.submit();
//                            }
//                        }
//                    }
        }, {
            fieldLabel: 'Password',
            name: 'password',
            inputType: 'password',
            allowBlank: false,
//                    listeners: {
//                        specialkey: function(field, event) {
//                            if (event.getKey() === event.ENTER) {
//                                var form = field.up('form').getForm();
//                                form.submit();
//                            }
//                        }
//                    }
        }],
    buttons: [{
            text: 'Login',
            formBind: true,
            disabled: true,
            handler: function() {

                var form = this.up('form').getForm();
                form.submit({
                    url: '/src/public/user/auth/authenticate',
                    success: function(form, action) {
                        if (action.result.success === 'true')
                        {
                            var user = action.result.user;

                            var userSetting = Ext.create('Intranet.Desktop.model.UserSetting', {
                                id: 0,
                                userID: user.id,
                                username: user.username,
                                first_name: user.first_name,
                                middle_name: user.middle_name,
                                last_name: user.last_name,
                                dob: user.dob,
                                email: user.email,
                                phone: user.phone,
                                avatar: user.avatar
                            });

                            userSetting.save();

                            

                            Ext.Ajax.request({
                                url: '/src/public/module/index/getExtController',
                                success: function(response) {
                                    var data = Ext.decode(response.responseText);

                                    if (data.success === 'true')
                                    {
                                        var controllers = data.controllers;

                                        var c;

                                        for (var i = 0; i < controllers.length; i++)
                                        {
                                            c = myApp.getController(controllers[i]);
                                            c.init();
                                        }

                                        Ext.create('Intranet.Desktop.view.MainScreen');
                                        //loadGeneralModules();
                                    }
                                    else
                                    {
                                        Ext.Msg.alert('Error', 'Error loading ext controller from database!');
                                    }

                                }
                            });

                            var login_window = Ext.getCmp('login-window');

                            login_window.destroy();
                        }
                        else
                        {
                            Ext.Msg.alert('Login failed', action.result.msg);
                        }
                    },
                    failure: function(form, action) {
                        Ext.Msg.alert('Login failed', 'Error ajax');
                    }

                });

            }
        }, {
            text: 'Cancel',
            handler: function() {
                this.up('form').getForm().reset();
            }
        }
    ]

});




